package com.cloud.elm.controller;

import com.cloud.elm.po.Cart;
import com.cloud.elm.service.CartService;
import com.cloud.elm.utils.response.ResultBody;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/elm/CartController")
public class CartController {

	@Autowired
	private CartService cartService;
	
	@GetMapping("/listCart/{userId}")
	public ResultBody<List<Cart>> listCart(@PathVariable("userId") String userId) {
		Cart cart = new Cart();
		cart.setUserId(userId);
		return ResultBody.success(cartService.listCart(cart));
	}

	@GetMapping("/listCart/{userId}/{businessId}")
	public ResultBody<List<Cart>> listCart(@PathVariable("userId") String userId,
										   @PathVariable("businessId") String businessId) {
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		return ResultBody.success(cartService.listCart(cart));
	}
	
	@PostMapping("/saveCart/{userId}/{businessId}/{foodId}")
	public ResultBody<Integer> saveCart(@PathVariable("userId") String userId,
										@PathVariable("businessId") String businessId,
										@PathVariable("foodId") Integer foodId) {
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		return ResultBody.success(cartService.saveCart(cart));
	}
	
	@PutMapping("/updateCart/{userId}/{businessId}/{foodId}/{quantity}")
	public ResultBody<Integer> updateCart(@PathVariable("userId") String userId,
										  @PathVariable("businessId") String businessId,
										  @PathVariable("foodId") Integer foodId,
										  @PathVariable("quantity") Integer quantity) {
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		cart.setQuantity(quantity);
		return ResultBody.success(cartService.updateCart(cart));
	}
	
	@DeleteMapping("/removeCart/{userId}/{businessId}/{foodId}")
	public ResultBody<Integer> removeCart(@PathVariable("userId") String userId,
										  @PathVariable("businessId") String businessId,
										  @PathVariable("foodId") Integer foodId) {
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		cart.setFoodId(foodId);
		return ResultBody.success(cartService.removeCart(cart));
	}
}
